﻿using PaceR.Core.Factories;
using PaceR.Core.Interfaces.PatternMatching;

namespace PaceR.Core.Utilities
{
    public sealed class PatternMatchFactoryUtil
    {
        public static volatile IPatternMatchFactory _factory;
        private static object syncRoot = new object();

        //private constructor to enforce singleton pattern
        private PatternMatchFactoryUtil()
        { }

        /// <summary>
        /// Concrete IPatternMatchFactory
        /// </summary>
        public static IPatternMatchFactory Factory
        {
            get
            {
                if (_factory == null)
                {
                    //set to default factory
                    lock (syncRoot)
                    {
                        if (_factory == null)
                            _factory = new SimplePatternMatchFactory();
                    }
                }
                return _factory;
            }
        }

        /// <summary>
        /// Sets the factory to be returned by this utility
        /// </summary>
        /// <param name="factory"></param>
        /// <remarks>Call this function before the factory has been called 
        /// else it is will be set to a default factory</remarks>
        public static void Intialize(IPatternMatchFactory factory)
        {
            lock (syncRoot)
            {
                if (_factory != null) _factory = factory;
            }
        }
    }
}
